package com.duoec.dw.lineage.service.analyser.from;

import com.alibaba.druid.sql.ast.statement.SQLUnionQueryTableSource;
import com.duoec.dw.lineage.dto.MultiTableLineageDto;
import com.duoec.dw.lineage.dto.TableLineageDto;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author xuwenzhen
 */
@Service
public class UnionQueryTableSourceAnalyser extends BaseTableSourceAnalyser<SQLUnionQueryTableSource> {
    /**
     * 获取当前类处理的类型
     *
     * @return SQLTableSource子类类型
     */
    @Override
    public Class<SQLUnionQueryTableSource> getTableSourceType() {
        return SQLUnionQueryTableSource.class;
    }

    /**
     * 解析 from
     *
     * @param tableSource         需要解析的from
     * @param withAsTableLineages with-as
     * @return 当前select脚本的血缘关系
     */
    @Override
    public MultiTableLineageDto analyse(SQLUnionQueryTableSource tableSource, MultiTableLineageDto withAsTableLineages) {
        TableLineageDto tableLineage = hiveAnalyser.analyseQuery(tableSource.getUnion(), withAsTableLineages);
        String alias = tableSource.getAlias();
        if (!StringUtils.isEmpty(alias)) {
            tableLineage.setAlias(alias);
        }
        MultiTableLineageDto tableLineages = new MultiTableLineageDto();
        tableLineages.add(tableLineage);
        return tableLineages;
    }
}
